﻿function Game() {
  var checkInterval = 50;

  var tower;
  var imageRepository;
  var canvas;
  var canvasContext;
  var canvasBuffer;
  var canvasBufferContext;

  this.initGameLoop = null;
  var gameLoop = null;
  var frameCount = 1;
  var FPS = 20;

  this.gameStates = {
    build: {value: 0, cycleTime: 0},
    fight: {value: 1, cycleTime: 19},
    win:   {value: 2},
    lose:  {value: 3}
  };
  var cycleTimeTotal = 20;
  var gameState = this.gameStates.build;

  var initialize = function () {
    imageRepository = new ImageRepository();
    tower = new Tower(imageRepository);
    canvas = document.getElementById('canvas');

    if (canvas && canvas.getContext) {
      canvasContext = canvas.getContext('2d');
      canvasBuffer = document.createElement('canvas');
      canvasBuffer.width = canvas.width;
      canvasBuffer.height = canvas.height;
      canvasBufferContext = canvasBuffer.getContext('2d');
      return true;
    }

    return false;
  }

  this.run = function () {
    if (initialize()) {
      imageRepository.LoadContent();
      tower.load(canvas);

      var game = this;
      $(document).bind('keypress',  function (event) {game.keyCheck(event);});
      $(document).bind('mousemove', function (event) {game.mouseMove(event);});
      $(document).bind('mousedown', function (event) {game.mouseDown(event);});

      this.initGameLoop = setInterval(this.initialUpdateRun, checkInterval);
    }
  }

  this.initialUpdateRun = function (ev) {
    if (tower.isLoaded() && imageRepository.IsLoaded()) {
      document.game.runGameLoop();

      //we don't need timer anymore
      clearInterval(document.game.initGameLoop);
      setInterval(document.game.runGameLoop, checkInterval);
    }
  }
  
  this.runGameLoop = function () {
    document.game.update();
    document.game.draw();
    document.game.incFrameCount();
  }

  this.incFrameCount = function () {
    frameCount++;
  }

  this.update = function () {
    tower.update(frameCount);

    var cycleTime = frameCount % cycleTimeTotal
    if (gameState != this.gameStates.win) {
      if (frameCount % FPS == 0) {
        tower.updateEnergy();
      }

      if (cycleTime == this.gameStates.build.cycleTime) {
        gameState = this.gameStates.build;
        tower.clearModes();
      } else if (cycleTime == this.gameStates.fight.cycleTime) {
        gameState = this.gameStates.fight;
        tower.clearModes();
      }
    }
  }

  this.keyCheck = function (event) {
    tower.keyPress(event, canvas, gameState);
  }

  this.mouseMove = function (event) {
    tower.mouseMove(event);
  }

  this.mouseDown = function (event) {
    tower.mouseDown(event);
  }

  this.draw = function () {
    //clear canvas
    canvasBufferContext.clearRect(0, 0, canvas.width, canvas.height);
    canvasContext.clearRect(0, 0, canvas.width, canvas.height);

    this.drawBackground(canvasContext);
    tower.draw(canvasContext, gameState);
    this.drawGUIOverlay(canvasContext);
/*
    //draw tower to buffer
    tower.draw(canvasBufferContext);

    //draw buffer on screen
    canvasContext.drawImage(canvasBuffer, 0, 0);*/
  }

  this.drawBackground = function (canvasContext) {
    switch (gameState) {
    case this.gameStates.build:
      canvasContext.fillStyle = $.Color([0.6, 0.5, 0.1], 'HSL').toHEX();
      break;
    case this.gameStates.fight:
      canvasContext.fillStyle = $.Color([0.6, 0.5, 0.6], 'HSL').toHEX();
      break;
    }
    canvasContext.fillRect(0, 0, canvas.width, canvas.height);
  }

  this.drawGUIOverlay = function (canvasContext) {
    canvasContext.fillStyle = "#000000";
    canvasContext.fillRect(10, 10, 80, 40);
    canvasContext.fillStyle = "#FFFFFF";
    canvasContext.fillText('LVL: ' + tower.levelCount, 15, 40);
    canvasContext.fillText('NRG: ' + tower.energy + ' (' + tower.energyRate + ')', 15, 25);

    if (gameState == this.gameStates.win) {
      canvasContext.fillText('WIN', canvas.width / 2, canvas.height / 2);
    } else if (gameState == this.gameStates.lose) {
      canvasContext.fillText('LOSE', canvas.width / 2, canvas.height / 2);
    }
  }

  this.win = function () {
    gameState = this.gameStates.win;
  }
}

$(document).ready(function () {
  document.game = new Game();
  document.game.run();
});
